package com.ruoyi.factory.qylock.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.qylock.service.InfoSynService;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import com.ruoyi.factory.qylock.utils.SignHelper;
import com.ruoyi.factory.qylock.utils.UicodeBackslashU;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName InfoSynServiceImpl
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2023/5/9 0009 13:54
 **/
@Service
public class InfoSynServiceImpl implements InfoSynService {
    protected final Logger logger = LoggerFactory.getLogger(InfoSynServiceImpl.class);


    /**
     * @author: Liujiaji
     * @date: 2023/4/26 0026 9:20
     * @description: TODO 用户信息同步
     * @param: []
     * @return: void
     **/
    @Override
    public void staff(String pageIndex) {
        //请求参数查询部门组织
        String url = "https://yonbip.diwork.com/iuap-api-gateway/yonbip/digitalModel/staff/list";
        String token = SignHelper.getToken();
        url = url+"?access_token="+token;
        Map<String, String> param = new HashMap<>();
        param.put("pageIndex",pageIndex);
        param.put("pageSize","500");
        String parm = JSONObject.fromObject(param).toString();
        //请求友空间获取信息
        String res = OkHttpUtils.okHttpPost(url, parm);
//        logger.info("请求友空间员工列表查询返参: "+res);
        String data = JSONObject.fromObject(res).optString("data");
        //处理返回数据插入部门表
        String content = JSONObject.fromObject(data).optString("recordList");
        JSONArray jsonArray = JSONArray.fromObject(content);

        List<Map<String, String>> list = new ArrayList<>();
        for (Object o : jsonArray) {
            String openUserId = JSONObject.fromObject(o).optString("user_id");
//            String openUserId = JSONObject.fromObject(o).optString("id"); 主键id
            String name = JSONObject.fromObject(o).optString("name");
            //enable 1在职即false 否则为true离职
            String enable = JSONObject.fromObject(o).optString("enable");
            String email = JSONObject.fromObject(o).optString("email");
            String thirdOrgId = JSONObject.fromObject(o).optString("dept_id");
            String mobile = JSONObject.fromObject(o).optString("mobile");
            if(StringUtils.isNotBlank(mobile) && mobile.contains("+86-")){
                String[] split = mobile.split("-");
                mobile = split[1];
            }
            String number = JSONObject.fromObject(o).optString("code");
            //整理参数传参
            Map<String, String> param2 = new HashMap<>();
            if(StringUtils.isEmpty(openUserId)) continue;
            param2.put("openUserId",openUserId);
            param2.put("name",name);
            String dismissed = enable.equals("1") ? "false" : "true";
            param2.put("dismissed",dismissed);
            //判断邮箱是否为空
            if(StringUtils.isNotEmpty(email)) param2.put("email",email);
            param2.put("thirdOrgId",thirdOrgId);
            param2.put("mobile",mobile);
            param2.put("number",number);
            //手机作为账号
            if(StringUtils.isNotEmpty(mobile)){
                param2.put("accountNo",mobile);
            }else{
                param2.put("accountNo",number);
            }
            list.add(param2);
        }
        String parm2 = JSONArray.fromObject(list).toString();
        String url2 = "/sync/emp";
//        logger.info("请求契约锁同步员工信息第"+pageIndex+"次入参: "+parm2);
        String res2 = OkHttpUtils.okHttpPostqys(url2, parm2);
        res2 = UicodeBackslashU.unicodeToCn(res2);
//        logger.info("请求契约锁同步员工信息第"+pageIndex+"次返参: "+res2);
        commitstaff();
//        logger.info("同步用户信息完成");
    }
    /**
     * @author: Liujiaji
     * @date: 2023/5/9 0009 20:02
     * @description: TODO 提交员工信息
     * @param: []
     * @return: void
     **/
    public void commitstaff(){
        String url = "/sync/now";
          Map<String, String> param = new HashMap<>();
        String res2 = OkHttpUtils.okHttpGetqys(url,param);
//        logger.info("请求契约锁提交更新员工信息返参: "+res2);
    }


}

